package postprocessing;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;

import org.apache.lucene.analysis.shingle.ShingleMatrixFilter.Matrix.Column;

public class Test{

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String pathname = "/local/joofeit/resultsSorted";
		File dir = new File(pathname);
		int column =0;
		SortedMap<String, String[]> map = new TreeMap<String, String[]>();
		if (dir.isDirectory()){
			File[] files = dir.listFiles();
			Collections.sort(Arrays.asList(files));
			for (File f: files){
				
				System.out.println(f.getName());
				String name = f.getName().split("__")[0].substring(8);
				System.out.println(name);
				column = setColumn(column, name);
				BufferedReader br = new BufferedReader(new FileReader(f));
				String line ="";
				int counter = 0;
				String[] list = new String[30];
				while ((line = br.readLine())!= null&&counter <30 ){
					String[] lineSplit = line.split(" ");
					String entry = lineSplit[0].substring(0,8)+" "+lineSplit[1];
					list[counter] = entry;
					column = setColumn(column, entry);
					counter++;
					
				}
				map.put(name, list);
				br.close();
				
			}
			makeTable(map, column);
			
		}
	}
	
	public static int setColumn (int column , String str){
		if (str.length()> column){
			column= str.length();
		}
		return column;
	}
	public static void makeTable(SortedMap<String, String[]> map, int column) throws IOException{
	String format ="";
	Object[] args = new String[map.keySet().size()];
	BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/local/joofeit/ResultsALL")));
	int counter =0;
	for(String idioms :map.keySet() ){
		format = format+" %-"+(column+3)+"s";
		args[counter] = idioms;
		counter++;
	}
	System.out.println(format.substring(1).split(",").length);
	System.out.println("\""+format.substring(1)+"\"");


	
	String line = String.format(format.substring(1),args);
	bw.write(line+"\n");
	System.out.println(line);
	for(int x =0;x<30;x++){
		Object[] curArgs = new String[map.keySet().size()];
		int idiomPointer =0;
		for (String idioms: map.keySet()){
			if(x+1<10){
			String cur =" "+Integer.toString(x+1)+" "+map.get(idioms)[x];
			curArgs[idiomPointer] = cur;

			}else{
			String cur =Integer.toString(x+1)+" "+map.get(idioms)[x];
			curArgs[idiomPointer] = cur;

			}
			idiomPointer++;
		}
		String line2 = String.format(format.substring(1),curArgs);

		bw.write(line2+"\n");
	}
	bw.flush();
	bw.close();
	}

}
